/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uit_vendor_management;

/**
 *
 * @author QuangKhoi
 */
public class Product {
    public String ProductID;
    public String ProductName;

    Database db = Database.instance;

    public Product(String _ProductID)
    {
        QueryResult result = db.Query("SELECT ProductID, ProductName FROM Product "
                + "WHERE ProductID = '" + ProductID + "'");
        if (result.Count == 1)
            ProductName = result.Table.getString(0, 0);
    }

    public Product(String ProductID, String Name)
    {
        ProductID = "M" + GetID();
        ProductName = Name;
    }

    public Integer GetID()
    {
        QueryResult result = db.Query("SELECT MAX(ProductID) FROM Product");
        if (result.Count == 1)
            return result.Table.getInteger(0, 0);
        return 1;
    }

    public Boolean Save()
    {
        QueryResult result = db.Query("SELECT * FROM Product WHERE ProductID='" + ProductID + "'");
        if (result.Count== 1)
        {
            //Update
            db.Query("UPDATE Product SET ProductName = " + ProductName + " WHERE ProductID = '" + ProductID + "'");
        }
        else
        {
            //Add New
            db.Query("INSERT INTO Product VALUE ('" + ProductID + "', '" + ProductName + "')");
        }
        return true;
    }

    public Boolean Delete()
    {
        db.Query("DELETE FROM Product WHERE ProductID='" + ProductID + "'");
        return true;
    }
}
